package com.jjb.common.view.draw;

import sun.font.FontDesignMetrics;

import java.awt.*;

public class TextDrawCanvas implements GrawCanvas {

    /**
     * 图片颜色
     */
    private final Color color;

    /**
     * 图片字体
     */
    private final Font font;

    /**
     * 写入图片的X坐标
     */
    private final int x;


    /**
     * 写入图片Y坐标
     */
    private final int y;


    /**
     * 写入内容
     */
    private String content;

    /**
     * 写入字体间距
     */
    private int space;
    /**
     *
     * @param x: 绘制的坐标X
     * @param y：绘制的坐标Y
     * @param content: 图像内容
     * @param
     */
    public TextDrawCanvas(int x,
                          int y,
                          String content,
                          Color markContentColor,
                          Font font) {
        this.x = x;
        this.y = y;
        this.content = content;
        this.color = markContentColor;
        this.font = font;
    }

    public int getSpace() {
        return space;
    }

    public void setSpace(int space) {
        this.space = space;
    }

    public static int getWidth(Font font, String content) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int width = 0 ;
        for(int i =0; i < content.length(); ++i) {
            width += metrics.charWidth(content.charAt(i));
        }
        return width;
    }

    public void draw(Graphics2D g) {
        g.setColor(this.color); //根据图片的背景设置水印颜色
        g.setFont(this.font);              //设置字体
        int currentX = this.x;
        int currenty = this.y;

        for(int i = 0; i < this.content.length(); ++i) {
            char sc = this.content.charAt(i);
            String  scontent = sc +"";
            g.drawString(scontent, currentX, currenty);  //画出
            currentX = currentX + getWidth(font, scontent) + this.space;
        }
    }
}
